
//------------------------------------------------------------------------------
pulse.window._UploadDataReader = function(meta, recordType){
    meta = meta || {};
    Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
};

Ext.extend( pulse.window._UploadDataReader, Ext.data.DataReader, {
  read : function( response )
  {
    var resp = pulse.server.parseResponse( response.responseText );
		if ( !resp.bExecuted )
			pulse.error( 'cannot upload content' );
    
    return {
	        success : resp.bExecuted,
	        records : [],
	        totalRecords : 0
	    };
  }
});

//------------------------------------------------------------------------------
pulse.window._UploadFileChooserForm = new Ext.form.FormPanel
({
	baseCls: 'x-plain',
	fileUpload: true,
	width: 500,
	frame: true,
	//title: 'File upload',
	autoHeight: true,
	bodyStyle: 'padding: 10px 10px 0 10px;',
	labelWidth: 50,
	onSubmit: Ext.emptyFn,
  /*submit: function() {
            pulse.window._UploadFileChooserForm.getForm().getEl().dom.submit();
        },*/
	defaults: {
	    anchor: '95%',
	    allowBlank: false,
	    msgTarget: 'side'
	},
	method: 'POST',
	reader: new pulse.window._UploadDataReader,
	errorReader: new pulse.window._UploadDataReader,
	items: [
		{
	    xtype: 'fileuploadfield',
	    id: 'form-file',
	    emptyText: 'Select a file on your computer',
	    fieldLabel: 'File',
	    name: 'uploaded-content',
	    buttonCfg: {
	        text: '',
	        iconCls: 'browse-icon'
	    }
	}]
});

//------------------------------------------------------------------------------
pulse.window.UploadFileChooser = new Ext.Window(
{
	title: 'Upload file',
	width: 500,
	minWidth: 300,
	closable: false,
	layout: 'fit',
	plain: true,
	resizable: false,
	modal: true,
	bodyStyle:'padding:5px;',
	buttonAlign:'center',
	items: pulse.window._UploadFileChooserForm,
	buttons: [
		{ 
			text: 'Upload', 
			handler: function()
			{
				if ( pulse.window._UploadFileChooserForm.getForm().isValid() )
 				{
					pulse.window._UploadFileChooserForm.getForm().submit({
		    		url: 'php/content.php?command=upload_content',
						//waitMsg: 'Uploading ...',
						success: function( fp, o ) {
    					pulse.info('File uploaded "'+o.result.file+'" on the server');
						},
    				failure: function( fp, o ) {
    					pulse.error('Cannot upload file : "'+o.result.file+'" on the server');
		    		}
					});
					pulse.window.UploadFileChooser.hide();
 				}
			}
		},
		{
			text: 'Cancel', 
			handler: function()
			{
				pulse.todo( 'use relative thing like "this" instead of the absolute path to the object' );
				pulse.window.UploadFileChooser.hide();
			}
		}]
});

